#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<list>
#include "DesignExtractor.h"
#include "Parser.h"
#include "AST_XLab2.h"
#include "SplitDeclaration.h"


using namespace std;

//void splitQuery(std::string query, std::vector<std::string>& results);

int main()
{
	string filename, query, declaration;
	STATUS status;
	COLLECTION varlist;
	COLLECTION* var=&varlist;
	vector<std::string> output;
	int isValidQuery;

	//cout<<"\nEnter File Name which contains the Simple Language Code";
	//getline(cin,filename);

	// Call Design Extractor

	AST *ast = new AST();
	
	// Call Parser function

	Parser p = Parser(ast);
	p.Parse("test.txt");
	// Call Design Extractor
	DesignExtractor DesExt(ast);
	DesExt.fillTable();

	VarTable *v = VarTable::getInstance();
	StatTable *s = StatTable::getInstance();

	// Call the declaration add function
	SplitDeclaration SpDc; 
	QueryEvaluator obj;
	QRYSTATEMENT_TYPE asstType;
	list<string> result;
	
	do{
		cout<<"Enter Declarations: ";
		getline(cin,declaration);

		asstType=SpDc.splitDeclaration(obj, declaration,var);

		if(asstType==_INVALID)
			cout<<"Invalid Declarations.\n";

	} while(asstType==_INVALID);
	

	//For the Query
	
	cout<<"Enter exit if you have no more queries";
	do{
		cout<<"\nEnter Query: ";
		getline(cin,query);

		// Call the Query Evaluator
		
		if(!query.compare("exit")==0)
		{

			if(!query.empty())
				isValidQuery = obj.validateQuery(query);
			else isValidQuery = -1;

			if(isValidQuery!=-1 && isValidQuery!=-2)
			{
				obj.executeQuery(result);
				
				//Print the output- list of integers saved in outputs.
				cout<<"\nOutput is as follows:\n";
				while(!result.empty() && result.size()!=1)
				{
					cout<<result.front();
					result.pop_front();
					cout<<", ";
				}
				if(!result.empty())
					cout<<result.front()<<"\n";
				result.clear();
			}

			else if(isValidQuery==-1) {
				cout<<"\n Invalid query.\n";
				}
			else if(isValidQuery==-2)
				cout<<"\n Empty query. \n";
		}

	} while(!(query.compare("exit")==0));

	getchar();
	return 0;
}

